package net.siufung.core.model;

import lombok.Data;
import lombok.EqualsAndHashCode;
import net.siufung.core.model.enums.MethodEvents;
import net.siufung.core.utils.ValidationUtil;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Objects;

/**
 * 验证重复实体
 * @author 陈建峰
 * @since 2022/2/4 11:16 下午
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class VerifyRepeatDto<PK> extends AbstractDto {

    private static final long serialVersionUID = -6874624642174009638L;

    /**
     * 验证的属性关键字
     */
    @NotBlank(message = "判重属性关键字不能为空！")
    private String verifyKeyValue;

    /**
     * 方法事件枚举
     */
    @NotNull(message = "判重请求方法不能为空！")
    private MethodEvents methodEvents;

    /**
     * 主键
     */
    private PK pk;

    /**
     * 租户Id
     */
    private String tenantId;

    /**
     * 额外扩展属性
     */
    private Map<String, Object> extra;

    @Override
    public Boolean validated() {
        Boolean valid = ValidationUtil.validated(this);
        if(MethodEvents.UPDATE.equals(methodEvents) ||
                MethodEvents.STATUS.equals(methodEvents) ){
            return valid && !Objects.isNull(pk);
        }
        return valid;
    }
}
